﻿/**
*	系统类
*
*/

var system = function () {
		var gameTitle = 'test',		// 游戏标题
			order = 0,				// 剧本指针
			debugFlag = true,		// 调试开关
			//isRunning = false,	// 脚本执行状态
			//isWaiting = false,	// 等待用户输入
			status = 'getscript',	// 游戏状态，有getscript，waitclick，runscript, menu四个状态
			// 执行单条脚本
			scripttimer,			// 脚本执行计时器
			executeScript = function (command) {
				var script = command.split("  ");
				switch(script[0]){
					case 'bg':
						bg.draw(script[1]);
						break;
					case 'bgb':
						bg.draw2(script[1]);
						break;
					case 'bgcolor':
						bg.drawcolor(script[1]);
						break;
					case 'bgfadein':
						var params = script[1].split(",");
						bg.fadein(params[0],params[1]);
						break;
					case 'bgfadeout':
						bg.fadeout(parseInt(script[1]));
						break;
					case 'bgfadeout2':
						var params = script[1].split(",");
						bg.fadeout2(params[0], parseInt(params[1]));
						break;
					case 'bgm':
						se.playBgm(script[1]);
						break;
					case 'bgmstop':
						se.stopBgm();
						break;
					case 'btof':
						bg.b2f();
					case 'c':
						km.waitClick();
						break;
					case 'caption':
						$(document)[0].title = script[1];
						break;
					case 'ci':
						ci.draw(script[1]);
						break;
					case 'ciclear':
						ci.clear();
						break;
					case 'cifadein':
						var params = script[1].split(",");
						ci.fadein(params[0], params[1]);
						break;
					case 'cifadeout':
					// 暂未实现
						break;
					case 'drawbtn':
						sm.drawBtn(script[1]);
						break;
					case 'grey':
						bg.greyfilter();
						break;
					case 'fontsize':
						tw.setFontSize(script[1]);
						break;
					case 'newbtn':
						var params = script[1].split(",");
						sm.newBtn(params[0], params[1], params[2], params[3]);
						break;
					case 'select':
						tw.selectText(script[1]);
						break;
					case 'sp':
						var params = script[1].split(",");
						var sp1 = sprite(100,100,params[2]);
						sp1.draw(params[0],params[1]);
						break;
					case 'transform':
						var params = script[1].split(",");
						bg.transform(params[0], params[1]);
						break;
					case 'text':
						tw.showText(script[1]);
						break;
					case 'textd':
						tw.showTextDirect(script[1]);
						break;
					default:
						alert('不认识的指令：' + command + ' 当前指令指针：' + order);
						clearInterval(scripttimer);
						break;
				};
			},
			debugMsg = function () {
				$('#debugmsg').append('指针：' + order + '\r\n' + '内容:' + scriptArray[order] + '\r\n');
			},
			that = {
				// 开始脚本执行状态
				getStatus: function () {
					return status;
				},
				// 改变游戏状态
				changeStatus: function (s) {
					status = s;
					if (debugFlag) {
						$('div#gamestatus').html(status);
					};
				},
				scriptLoop: function () {
					scripttimer = setInterval(function(){
						if (status == 'getscript'){
							if (debugFlag) {
								$('#debug').css({'display':'block'});
								debugMsg();
							};
							executeScript(scriptArray[order]);
							order += 1;
							if (order >= scriptArray.length){
								clearInterval(scripttimer);
								//alert('脚本已执行完毕');
							};
						};
					},100);
				}
			};
		return that;
	};


